package com.rabbiter.oes.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.entity.Teacher;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.TeacherServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "教师")
@RestController
public class TeacherController {

    @Autowired
    private TeacherServiceImpl teacherService;

    @ApiOperation(value = "分页查询教师信息", notes = "分页查询教师信息", httpMethod = "GET")
    @GetMapping("/teachers/{page}/{size}/{name}/{sex}/{tel}/{cardId}")
    public R<IPage<Teacher>> findAll(@PathVariable Integer page, @PathVariable("size") Integer size, @PathVariable("name") String name, @PathVariable("sex") String sex, @PathVariable("tel") String tel, @PathVariable("cardId") String cardId) {
        Page<Teacher> teacherPage = new Page<>(page, size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage, name, sex, tel, cardId);
        return R.<IPage<Teacher>>ok("分页查询教师信息").data(teacherIPage);
    }

    @ApiOperation(value = "根据id查询教师信息", notes = "根据id查询教师信息", httpMethod = "GET")
    @GetMapping("/teacher/{id}")
    public R<Teacher> findById(@PathVariable("id") Integer id) {
        Teacher teacher = teacherService.findById(id);
        return R.<Teacher>ok().data(teacher);
    }

    @ApiOperation(value = "根据id删除教师信息", notes = "根据id删除教师信息", httpMethod = "DELETE")
    @DeleteMapping("/teacher/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        teacherService.deleteById(id);
        return R.ok("删除成功");
    }

    @ApiOperation(value = "修改教师信息", notes = "修改教师信息", httpMethod = "PUT")
    @PutMapping("/teacher")
    public R update(@RequestBody Teacher teacher) {
        teacherService.update(teacher);
        return R.ok();
    }

    @ApiOperation(value = "修改教师密码", notes = "修改教师密码", httpMethod = "PUT")
    @PutMapping("/tacherUpdatePwd")
    public R updateByPassword(@RequestBody Teacher teacher) {
        teacherService.updateByPassword(teacher);
        return R.ok();
    }

    @ApiOperation(value = "添加教师信息", notes = "添加教师信息", httpMethod = "POST")
    @PostMapping("/teacher")
    public R add(@RequestBody Teacher teacher) {
        String acc = teacher.getAcc();
        Long countByAcc = teacherService.findCountByAcc(acc);
        if (countByAcc == 0) {
            teacherService.add(teacher);
            return R.ok().data(true);
        } else {
            return R.ok("账号已存在！").data(false);
        }
    }

    @ApiOperation(value = "重置管理员密码", notes = "重置管理员密码", httpMethod = "DELETE")
    @GetMapping("/tacher/resetPsw/{id}/{oldPsw}/{newPsw}")
    public R resetPsw(@PathVariable("id") Integer id, @PathVariable("newPsw") String newPsw, @PathVariable("oldPsw") String oldPsw) {
        try {
            teacherService.resetPsw(id, newPsw, oldPsw);
            return R.ok("修改密码成功！").data(true);
        }catch (Exception e) {
            e.printStackTrace();
            return R.ok(e.getMessage()).data(false);
        }
    }
}
